package project_package;

import java.util.ArrayList;
public class RentRecords
{
	private static RentRecords instance = null;
	ArrayList<Rent> rents = new ArrayList<Rent>();
	
	
	public static RentRecords getInstance()
	{
		if(instance == null)
		{
			instance = new RentRecords();
		}
		return instance;
	}
	
	// creates a new object/rent item 
	// if we buy a new car which can be leased, we create a new object of that car
	// if we lease it, we update it with the current information every time it's leased and returned
	public void createRent(String name, double price, Customer customer, Employee employee)
	{
		int nextID = rents.size() +1; // next available ID
		Rent newRent = new Rent(nextID, name, 0, 0, 0, customer, price, false, employee);
		addRent(newRent);
	}
	
	// this is how we lease a rent
	public void leaseRent(Rent rent, Customer customer, Employee employee, long leasePeriod) 
	{
		if(rent != null)
		{
			if (rent.getIsLeased())
			{
				System.out.println("Rent #"+rent.getID()+" is already leased to "+rent.getCustomer()+" until "+rent.getLeaseReturnDateString());
			} 
			else {
				long timestamp = System.currentTimeMillis()/1000; // unix timestamp format
				
				rent.setCustomer(customer);
				rent.setEmployee(employee);
				rent.setLeasePeriod(leasePeriod); // period in days
				rent.setLeaseDate(timestamp); // current time
				rent.setLeaseReturned(0); // when was it returned? we just leased it, it's not returned yet!
				rent.setIsLeased(true);
				System.out.println("Rent #"+rent.getID()+" was leased to "+customer.getName()+" until "+rent.getLeaseReturnDateString());
			}
		} 
		else {
			System.out.println("Specify a rent, please"); 
		}
		
	}
	
	// add the rent to the list
	public void addRent(Rent rent)
	{
		if(rent != null)
		{
			rents.add(rent);
			System.out.println("Rent was added successfully");
		} 
		else {
			System.out.println("Specify a rent, please"); 
		}
	}
	
	// return a lease
	public void returnRent(Rent rent)
	{
		if(rent != null)
		{
			if (!rent.getIsLeased()) 
			{
				System.out.println("Rent #"+rent.getID()+" is not leased");
			} 
			else {
				long timestamp = System.currentTimeMillis()/1000;
				
				rent.setCustomer(null);
				rent.setEmployee(null);
				rent.setLeasePeriod(0); // no longer leased
				rent.setLeaseDate(0); // no longer leased
				rent.setLeaseReturned(timestamp); // we just got it back
				rent.setIsLeased(false);
				System.out.println("Rent #"+rent.getID()+" was returned");
			}
		} 
		else {
			System.out.println("Specify a rent, please"); 
		}
	}
	
	// delete a rent from the list
	public void deleteRent(int id)
	{
		boolean found = false;
		for(int i=0; i<rents.size();i++) 
		{
			if (id == rents.get(i).getID())
			{
				rents.remove(rents.get(i));
				System.out.println("Rent #"+rents.get(i).getID()+" was removed from the list");
				found = true;
			}
		}
		
		if (!found) 
			System.out.println("No rent with ID #"+id+" was found");
	}
	
	// find rent by id
	public Rent findRentID(int id)
	{
        Rent back;
        if (rents.isEmpty())
        {
            System.out.println("No rents in the database");
            return null;
        }
        else {
            for (int i = 0; i < rents.size(); i++) 
            {
                if (rents.get(i).getID() == id)
                {
                    back = rents.get(i);
                    return back;
                }
            }
        }
        System.out.println("Rent with ID #"+id+" was not found");
        return null;
    }
	
	// find rent by name
	public Rent findRentName(String name)
	{
        Rent back;
        if (rents.isEmpty())
        {
            System.out.println("No rents in the database");
            return null;
        }
        else {
            for (int i = 0; i < rents.size(); i++) 
            {
                if (rents.get(i).getName().equals(name))
                {
                    back = rents.get(i);
                    return back;
                }
            }
        }
        System.out.println("Rent with name #"+name+" was not found");
        return null;
    }
	
	// print all rents
	public void printRents()
	{
		if (rents.isEmpty()) {
            System.out.println("No rents in the database");			
		}
		else {
			for(int i = 0; i < rents.size(); i++)
			{
				Rent printRent = rents.get(i);
				System.out.print("Rent ID: " + printRent.getID() + " - Rent Name: " + printRent.getName() + " - Rent Price: " + printRent.getPrice() + " - ");
				if (printRent.getIsLeased()) 
				{
					System.out.print("Customer: " + printRent.getCustomer().getName() + "Leased on: " + printRent.getLeaseDate() + " - Leased for " + printRent.getLeasePeriod() + "day(s) - Leased until" + printRent.getLeaseReturned());
				}
				else {
					System.out.print("Available to be rent");		
				}
				System.out.println();
			}
		}
	}
	
	public boolean checkIfNoRents()
	{
		if(rents.isEmpty())
		{
			return false;
		} else {
			return true;
		}
		
	}
	public String printUnavabRentID()
	{
		String back = "";
		for (int i = 0; i < rents.size(); i++)
		{
			Rent printRent = rents.get(i);
			if(printRent.getIsLeased())
			{
				printRent.getID();
				back = Integer.toString(printRent.getID());
			}
		}
		return back;
	}
	
	public String printAvabRentID()
	{
		String back = "" ;
		for(int i = 0 ; i < rents.size(); i++)
		{
			Rent printRent = rents.get(i);
			if(!printRent.getIsLeased())
			{
				back = Integer.toString(printRent.getID());
			}
		}
		return back;
	}
	public String printUnavabRentName()
	{
		String back = "";
		for(int i = 0 ; i < rents.size(); i++)
		{
			Rent printRent = rents.get(i);
			if(printRent.getIsLeased())
			{
				back = printRent.getName();
			}
		}
		return back;
	}
	public String printAvabRentName()
	{
		String back = "";
		for (int i = 0; i < rents.size() ; i++)
		{
			Rent printRent = rents.get(i);
			if(!printRent.getIsLeased())
			{
				back = printRent.getName();
			}
		}
		return back;
	}
	public String printUnavabRentPrice()
	{
		String back = "";
		for ( int i = 0; i < rents.size(); i++ )
		{
			Rent printRent = rents.get(i);
			if(printRent.getIsLeased())
			{
				back = Double.toString(printRent.getPrice());
			}
		}
		return back;
	}
	public String printAvabRentPrice()
	{
		String back = "";
		for ( int i = 0; i < rents.size(); i++)
		{
			Rent printRent = rents.get(i);
			if(printRent.getIsLeased())
			{
				back = Double.toString(printRent.getPrice());
			}
		}
		return back;
	}
	public String printCustNameRent()
	{
		String back = "";
		for (int i = 0; i < rents.size(); i++)
		{
			Rent printRent = rents.get(i);
			back = printRent.getCustomer().getName();
		}
		return back;
	}
	public String printCustDateRent()
	{
		String back = "";
		for ( int i = 0; i < rents.size(); i++)
		{
			Rent printRent = rents.get(i);
			back = Long.toString(printRent.getLeaseDate());
		}
		return back;
	}
	public String printCustDurationRent()
	{
		String back = "";
		for ( int i = 0; i < rents.size(); i++)
		{
			Rent printRent = rents.get(i);
			back = Long.toString(printRent.getLeasePeriod());
		}
		return back;
	}
	public String printCustSetReturn()
	{
		String back = "";
		for ( int i = 0; i < rents.size() ; i++)
		{
			Rent printRent = rents.get(i);
			back = Long.toString(printRent.getLeaseReturned());
		}
		return back;
	}
	
	
	// print all available rents
	public void printRentsAvailable()
	{
		if (rents.isEmpty()) {
            System.out.println("No rents in the database");			
		}
		else {
			boolean found = false;
			for(int i = 0; i < rents.size(); i++)
			{
				Rent printRent = rents.get(i);
				if (!printRent.getIsLeased())
				{
					System.out.println("Rent ID: " + printRent.getID() + " - Rent Name: " + printRent.getName() + " - Rent Price: " + printRent.getPrice());
					found = true;
				}
			}
			
			if (!found)
				System.out.println("There were no rents available");
		}
	}
	
	// print info for only 1 rent
	public void printRent(int id)
    {
        Rent printRent;
        for (int i = 0; i < rents.size(); i++) 
        {
            if (rents.get(i).getID() == id)
            {
            	printRent = rents.get(i);
            	System.out.print("Rent ID: " + printRent.getID() + " - Rent Name: " + printRent.getName() + " - Rent Price: " + printRent.getPrice() + " - ");
            	if (printRent.getIsLeased()) 
            	{
            		System.out.print("Customer: " + printRent.getCustomer().getName() + "Leased on: " + printRent.getLeaseDateString() + " - Leased for " + printRent.getLeasePeriod() + "day"+ (printRent.getLeasePeriod()==1 ? "" : "s") +" - Leased until" + printRent.getLeaseReturnedString());
                }
                else {
                	System.out.print("Available to be rent");       
                }
                System.out.println();
            }
        } 
	}
}